using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace OnlineAnadolu
{
    public partial class ozelmesaj : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["usertype"].ToString() == "")
            {
                Response.Redirect("HataSayfasi.aspx");
            }
            if (Session["us"].ToString() != "")
                tb_receiver.Text = Session["us"].ToString();
                 


            //if(Request.QueryString["CatID"].ToString()!="")
            //    tb_receiver.Text = Request.QueryString["CatID"].ToString();

        }

        protected void bt_send_Click(object sender, EventArgs e)
        {
            //SqlDataSource1.InsertCommand = "INSERT INTO sendmessage(sender, receiver, subject, message) VALUES (N'" + Session["username"].ToString() + "',N'" + tb_receiver.Text + "',N'" + tb_subject.Text + "',N'" + tb_message.Text + "')";
            //SqlDataSource1.Insert();

            string query = "SELECT [user_name] FROM [users] WHERE (([user_name] = N'" + tb_receiver.Text + "') )";
    
            SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["shoppingConnectionString2"].ToString());
            SqlCommand cmd2 = new SqlCommand(query, con2);
            SqlDataAdapter ad2 = new SqlDataAdapter(cmd2);
            DataTable dt2 = new DataTable();
            ad2.Fill(dt2);
            if (dt2.Rows.Count == 0)
            {
                lb_err.Text = "yok olm boyle biri";
               
            }
            else
            {

                SqlDataSource1.InsertCommand = "INSERT INTO sendmessage(sender,receiver,subject,message,tarih,tip)VALUES(N'" + Session["username"].ToString() + "',N'" + tb_receiver.Text + "',N'" + tb_subject.Text + "',N'" + tb_message.Text + "','" + DateTime.Now.ToString("MM.dd.yyyy HH:mm:ss") + "',1)";

                SqlDataSource1.Insert();
                SqlDataSource1.InsertCommand = "INSERT INTO sendmessage(sender,receiver,subject,message,tarih,tip)VALUES(N'" + Session["username"].ToString() + "',N'" + tb_receiver.Text + "',N'" + tb_subject.Text + "',N'" + tb_message.Text + "','" + DateTime.Now.ToString("MM.dd.yyyy HH:mm:ss") + "',2)";
                SqlDataSource1.Insert();
                tb_message.Text = "";
                tb_receiver.Text = "";
                tb_subject.Text = "";
                lb_err.Text="yolladim gibi ama cevap gelmezse uzulme";

            }
            

        }

        protected void bt_Kullanicilar_Click(object sender, EventArgs e)
        {
           //this.Response.Write("<script language=javascript>window.open('Kullanicilar.aspx',null,'width=900,height=500,resizable=1')</script>");
            Response.Redirect("Kullanicilar.aspx");
        }
    }
}
